package com.waypolice.manager.service.impl;

import java.util.Date;
import java.util.List;


import com.waypolice.common.pojo.TaotaoResult;

import com.waypolice.common.utils.DateTimeUtil;
import com.waypolice.common.utils.MathUtils;
import com.waypolice.mapper.*;
import com.waypolice.mapper.handwriting.UpmsRoleMapperSon;
import com.waypolice.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.waypolice.manager.service.RoleService;

/**
 * 角色实现类
 * @author wr
 *
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private UpmsUserRoleMapper upmsUserRoleMapper;

	@Autowired
	private UpmsRoleMapper upmsRoleMapper;

	@Autowired
	private UpmsRolePermissionMapper upmsRolePermissionMapper;

	@Autowired
	private UpmsPermissionMapper upmsPermissionMapper;

	@Autowired
	private UpmsRoleMapperSon upmsRoleMapperSon;

	@Override
	public List<UpmsRole> findAll() {
		UpmsRoleExample upmsRoleExample = new UpmsRoleExample();
		return upmsRoleMapper.selectByExample(upmsRoleExample);
	}

	@Override
	public List<UpmsUserRole> findByUserId(Integer useId) {
		UpmsUserRoleExample upmsUserRoleExample = new UpmsUserRoleExample();
		UpmsUserRoleExample.Criteria criteria = upmsUserRoleExample.createCriteria();
		criteria.andUserIdEqualTo(useId);
		List<UpmsUserRole> upmsUserRoles = upmsUserRoleMapper.selectByExample(upmsUserRoleExample);
		return upmsUserRoles;
	}

	@Override
	public TaotaoResult insertRole(String roleName, String roleDescription, String resourcesCode) {
		TaotaoResult result = new TaotaoResult();
		UpmsRole upmsRole = new UpmsRole();
		upmsRole.setName(roleName);
		upmsRole.setDescription(roleDescription);
		upmsRole.setCtime(DateTimeUtil.newDateToLong());
		int i=0;
		 i = upmsRoleMapperSon.insertUpmsRoleReturnRoleId(upmsRole);
		if(i != 0){
			Integer roleId = upmsRole.getRoleId();
			int i1 = insertRoleResource(roleId, resourcesCode);
			if(i1 != 1){
				result.setStatus(101);
				result.setMsg("添加角色关联资源失败");
			}else{
				result.setStatus(200);
				result.setMsg("添加成功");
			}
		}else{
			result.setStatus(101);
			result.setMsg("添加角色失败");
		}
		return result;
	}

	public int insertRoleResource(Integer roleId ,String resourcesCode){
		String[] recode = resourcesCode.split(",");
		int insert = 0;
		for (String string : recode) {
			UpmsPermissionExample upmsPermissionExample = new UpmsPermissionExample();
			UpmsPermissionExample.Criteria criteria2 = upmsPermissionExample.createCriteria();
			criteria2.andNameEqualTo(string);
			List<UpmsPermission> upmsPermissions = upmsPermissionMapper.selectByExample(upmsPermissionExample);
			Integer permissionId = upmsPermissions.get(0).getPermissionId();
			//判断是否为父级菜单
			if(upmsPermissions.get(0).getPid() == 0){
				UpmsRolePermission upmsRolePermission = new UpmsRolePermission();
				upmsRolePermission.setPermissionId(permissionId);
				upmsRolePermission.setRoleId(roleId);
				insert = upmsRolePermissionMapper.insertSelective(upmsRolePermission);
			}else {
				UpmsPermission upmsPermission = upmsPermissionMapper.selectByPrimaryKey(permissionId);
				Integer pid = upmsPermission.getPid();
				UpmsRolePermission upmsRolePermission3 = new UpmsRolePermission();
				upmsRolePermission3.setPermissionId(pid);
				upmsRolePermission3.setRoleId(roleId);
				insert = upmsRolePermissionMapper.insert(upmsRolePermission3);
			}
		}
		return insert;
	}
	@Override
	public TaotaoResult editRole(String roleName, String roleDescription, String resourcesCode, String roleId) {
	    TaotaoResult result = new TaotaoResult();
		UpmsRolePermissionExample upmsRolePermissionExample = new UpmsRolePermissionExample();
		UpmsRolePermissionExample.Criteria criteria1 = upmsRolePermissionExample.createCriteria();
		criteria1.andRoleIdEqualTo(Integer.valueOf(roleId));
		List<UpmsRolePermission> upmsRolePermissions = upmsRolePermissionMapper.selectByExample(upmsRolePermissionExample);

		int i = 0;
		for (UpmsRolePermission upmsRolePermission : upmsRolePermissions) {
			int i1 = upmsRolePermissionMapper.deleteByPrimaryKey(upmsRolePermission.getRoleId());
			if(i!=1){
				result.setMsg("删除角色关联关系失败");
				result.setStatus(101);
				return result;
			}
		}
		UpmsRole upmsRole = upmsRoleMapper.selectByPrimaryKey(Integer.valueOf(roleId));
		upmsRole.setCtime(DateTimeUtil.newDateToLong());
		upmsRole.setName(roleName);
		upmsRole.setDescription(roleDescription);
		int i3 = upmsRoleMapper.updateByPrimaryKey(upmsRole);
		if(i3 != 1){
			result.setMsg("编辑角色信息失败");
			result.setStatus(101);
		}else{
			int i2 = insertRoleResource(Integer.parseInt(roleId), resourcesCode);
			if(i2 != 1){
				result.setMsg("编辑角色信息失败");
				result.setStatus(102);
			}else{
                result.setMsg("编辑角色信息成功");
                result.setStatus(200);
            }
		}
		return result;
	}

	@Override
	public UpmsRole findRoleByRoId(String roId) {
		UpmsRole role = upmsRoleMapper.selectByPrimaryKey(Integer.parseInt(roId));
		return role;
	}

	/**
	 * @author:zyp
	 * @description 查看当前名字是否重复
	 * @date: 2019/5/27 9:04
	 * @return 
	 */
	@Override
	public int haveRoleName(String roleName, String roleId) {
		//如果角色id为null，则为新建角色，查看当前角色表中是否有相同名字的数据
		UpmsRoleExample upmsRoleExample = new UpmsRoleExample();
        UpmsRoleExample.Criteria criteria = upmsRoleExample.createCriteria();
        if(roleId==null){
            criteria.andNameEqualTo(roleName);
		}else{
            criteria.andNameEqualTo(roleName);
            criteria.andRoleIdNotEqualTo(MathUtils.string2int(roleId));
        }
        int count = upmsRoleMapper.countByExample(upmsRoleExample);
        return count;
	}
}
